package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bean.Role;
import com.example.bean.RoleMenu;
import com.example.mapper.RoleMenuMapper;
import com.example.service.RoleMenuService;
import com.example.service.RoleService;
import com.example.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author wjf
* @description 针对表【sys_role】的数据库操作Service实现
* @createDate 2022-09-26 16:48:03
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired
    private RoleMenuService roleMenuService;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Transactional
    @Override
    public void updateRoleMenu(Integer roleId, List<Integer> menuIds) {
        //先删除当前角色id所有的绑定关系
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        roleMenuService.remove(queryWrapper);

        //再把前端传过来的菜单id数组绑定到当前的这个角色id上去
        for (int i:menuIds){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(i);
            roleMenuService.save(roleMenu);
        }
    }

    @Override
    public List<Integer> getMenu(Integer roleId) {
        List<Integer> list=  roleMenuMapper.getMenuId(roleId);
        return list;
    }
}




